var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var voteUrl = require("url/vote");
var qqHttp = require("helper/http");
var helperOpen = require("helper/open");
var noData = [{"template":1,"labelContent":"暂无成员投票"}];
var pageNum = 1, totalPages = 1;
// 本页面-ajax业务请求url
var httpUrl = voteUrl.url.result.memberVote;

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
var listView = ui("ListView");
var listdata = mm("do_ListData");
listView.bindItems(listdata);
// 定义变量
var page = 1;
var type;

// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
headerRight.visible = false;
headerLeft.on("touch", function() {
	do_App.closePage()
});
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "投票成员"
})
header.refreshData();
// 页面装载完成后，开始初始化工作
var voteItemId;
do_Page.on("loaded", function() {
	deviceone.print(voteItemId,"voteItemId")
	voteItemId=do_Page.getData().voteItemId;
	laodPage();
});

// 刷新数据
function laodPage() {
	page = 1;
	var params = {
		page : page,
		voteItemId:voteItemId
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.removeAll();
		if (data.page.content.length>0) {
			listdata.addData(data.page.content);
		} else {
			listdata.addData(noData);
		}
		listView.refreshItems();
	})
}
//刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page,
		voteItemId:voteItemId
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.addData(data.page.content);
		listView.refreshItems();
	})
}

function getNextPageData(){
	pageNum++;
	if (pageNum <= totalPages) {
		qqHttp.post(userUtil.url.voteResult.page, {page:pageNum}, function(data) {
			totalPages = data.page.totalPages;
			listdata.addData(data.page.content);
			listView.rebound();
			listView.refreshItems();
		})
	} else {
		nf.toast("已是最后一页");
		listView.rebound();
		listView.refreshItems();
	}
}


//下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		laodPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});
